﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sieuthi
{
    public partial class chitiet : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private SqlConnection conn = new SqlConnection(Threelayer.VO.core.connectionsring.ToString());
        private SqlCommand cmd = new SqlCommand();
        private DataTable dt = new DataTable("chitiet");
        Threelayer.VO.chitietVO ct_vo;
        Threelayer.BUS.chitietBUS ct_bus;
        public chitiet()
        {
            InitializeComponent();
        }

        private void chitiet_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sieuthiDataSet2.mathang' table. You can move, or remove it, as needed.
            this.mathangTableAdapter1.Fill(this.sieuthiDataSet2.mathang);
            // TODO: This line of code loads data into the 'sieuthiDataSet1.mathang' table. You can move, or remove it, as needed.
           // this.mathangTableAdapter.Fill(this.sieuthiDataSet1.mathang);
            // TODO: This line of code loads data into the 'sieuthiDataSet2.hoadon' table. You can move, or remove it, as needed.
            this.hoadonTableAdapter.Fill(this.sieuthiDataSet2.hoadon);
            chitietdatabinding();
            getdata();
        }
        private void chitietdatabinding()
        {
            box_gia.DataBindings.Clear();
            box_gia.DataBindings.Add("text", dataGridchitiet.DataSource, "gia");
            box_mahd.DataBindings.Clear();
            box_mahd.DataBindings.Add("text", dataGridchitiet.DataSource, "mahd");
            box_sl.DataBindings.Clear();
            box_sl.DataBindings.Add("text", dataGridchitiet.DataSource, "soluong");
            box_mamh.DataBindings.Clear();
            box_mamh.DataBindings.Add("text", dataGridchitiet.DataSource, "mamh");
        }
        private void data_chitiet()
        {
            ct_vo = new Threelayer.VO.chitietVO();
            ct_vo.Gia = Convert.ToInt32(box_gia.Text);
            ct_vo.MaHD = box_mahd.Text;
            ct_vo.MaMH = box_mahd.Text;
        }
        private void getdata()
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "chitiet_xem";
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridchitiet.DataSource = dt;
            conn.Close();
        }
        private void bt_chitiet_them_Click(object sender, EventArgs e)
        {
            data_chitiet();
            ct_bus = new Threelayer.BUS.chitietBUS();
            ct_bus.chitiet_them(ct_vo);
            chitiet_Load(sender, e);
            chitietdatabinding();
        }

        private void bt_chitiet_sua_Click(object sender, EventArgs e)
        {
            data_chitiet();
            ct_bus = new Threelayer.BUS.chitietBUS();
            ct_bus.chitiet_sua(ct_vo);
            chitiet_Load(sender, e);
            chitietdatabinding();
        }

        private void bt_chitiet_xoa_Click(object sender, EventArgs e)
        {
            data_chitiet();
            ct_bus = new Threelayer.BUS.chitietBUS();
            ct_bus.chitiet_xoa(box_mahd.Text);
            chitiet_Load(sender, e);
            chitietdatabinding();
        }

        private void bt_chitiet_timkiem_Click(object sender, EventArgs e)
        {

            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "chitiet_timkiem";
            cmd.Parameters.Add("@mahd", SqlDbType.NVarChar, 200).Value = box_chitiet_timkiem.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridchitiet.DataSource = dt;
            conn.Close();
        }
        private void box_chitiet_timkiem_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                menuhelp _mn = new menuhelp();
                _mn.Show();
            }
        }

    }
}
